package com.quectel.core.module.device.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.core.module.device.dao.DeviceCurrentAlarmDao;
import com.quectel.core.module.device.dto.DeviceCurrentAlarmDto;
import com.quectel.core.module.device.entity.DeviceCurrentAlarmEntity;
import com.quectel.core.module.device.service.DeviceCategoryService;
import com.quectel.core.module.device.service.DeviceCurrentAlarmService;
import com.quectel.core.module.device.service.DeviceService;
import com.quectel.core.module.device.service.DeviceTypeService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.Snowflake;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-06-14 15:14:16
 */
@DubboService
public class DeviceCurrentAlarmServiceImpl implements DeviceCurrentAlarmService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DeviceCurrentAlarmServiceImpl.class);

    @Autowired
    private DeviceCurrentAlarmDao deviceCurrentAlarmDao;
    @Autowired
    private DeviceCategoryService deviceCategoryService;
    @Autowired
    private DeviceTypeService deviceTypeService;
    @Autowired
    private DeviceService deviceService;

    @Override
    public List<DeviceCurrentAlarmDto> queryListLessPadding(Map<String, Object> params) {
        List<DeviceCurrentAlarmEntity> list = deviceCurrentAlarmDao.queryList(params);
        List<DeviceCurrentAlarmDto> result = CopyUtils.copyList(list, DeviceCurrentAlarmDto.class);
        result.forEach(this::empPaddingFiled);
        return result;
    }

    private void empPaddingFiled(DeviceCurrentAlarmDto dto) {
        if (dto != null) {
            if (dto.getDeviceId() != null) {
                dto.setDevice(deviceService.selectByIdWithoutPadding(dto.getDeviceId()));
            }
            if (dto.getDeviceTypeId() != null) {
                dto.setDeviceType(deviceTypeService.selectCacheById(dto.getDeviceTypeId()));
            }
            if (dto.getDeviceCategoryId() != null) {
                dto.setDeviceCategory(deviceCategoryService.selectCacheById(dto.getDeviceCategoryId()));
            }
        }
    }

    @Override
    public List<DeviceCurrentAlarmDto> queryByDeviceId(Long deviceId) {
        List<DeviceCurrentAlarmEntity> deviceCurrentAlarmEntityList = deviceCurrentAlarmDao.selectList(
                new QueryWrapper<DeviceCurrentAlarmEntity>().eq("device_id", deviceId));
        return CopyUtils.copyList(deviceCurrentAlarmEntityList, DeviceCurrentAlarmDto.class);
    }

    @Override
    public DeviceCurrentAlarmDto queryByDeviceIdAndRuleId(Long deviceId, Long ruleId) {
        DeviceCurrentAlarmEntity deviceCurrentAlarmEntity = deviceCurrentAlarmDao.selectOne(
                new QueryWrapper<DeviceCurrentAlarmEntity>().eq("device_id", deviceId).eq("rule_id", ruleId).last(SqlConstants.MYSQL_LIMIT_ONE));
        return CopyUtils.copyObj(deviceCurrentAlarmEntity, DeviceCurrentAlarmDto.class);
    }

    @Override
    public List<DeviceCurrentAlarmDto> queryList(Map<String, Object> params) {
        List<DeviceCurrentAlarmEntity> list = deviceCurrentAlarmDao.queryList(params);
        List<DeviceCurrentAlarmDto> result = CopyUtils.copyList(list, DeviceCurrentAlarmDto.class);
        result.stream().forEach(dto -> {
            dto.setDeviceCategory(deviceCategoryService.selectById(dto.getDeviceCategoryId()));
            dto.setDeviceType(deviceTypeService.selectById(dto.getDeviceTypeId()));
            dto.setDevice(deviceService.selectById(dto.getDeviceId()));
        });
        return result;
    }


    @Override
    public int queryTotal(Map<String, Object> params) {
        return deviceCurrentAlarmDao.queryTotal(params);
    }

    @Override
    public Long save(DeviceCurrentAlarmDto dto) {
        DeviceCurrentAlarmEntity entity = CopyUtils.copyObj(dto, DeviceCurrentAlarmEntity.class);
        entity.setId(Snowflake.nextId());
        deviceCurrentAlarmDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(DeviceCurrentAlarmDto dto) {
        DeviceCurrentAlarmEntity entity = CopyUtils.copyObj(dto, DeviceCurrentAlarmEntity.class);
        deviceCurrentAlarmDao.updateById(entity);
    }

    @Override
    public DeviceCurrentAlarmDto selectById(Long id) {
        DeviceCurrentAlarmEntity entity = deviceCurrentAlarmDao.selectById(id);
        return CopyUtils.copyObj(entity, DeviceCurrentAlarmDto.class);
    }

    @Override
    public void deleteById(Long id) {
        deviceCurrentAlarmDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }

}
